package com.msmall.community.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.Constants;
import com.msmall.community.common.dao.UserAgentDao;
import com.msmall.community.common.model.UserAgent;
import com.msmall.community.common.service.UserAgentManager;


public class UserAgentManagerImpl extends BaseManagerImpl<UserAgent, Long> implements UserAgentManager{
	
	private static final Log logger = LogFactory.getLog(UserAgentManagerImpl.class);
	
	private UserAgentDao userAgentDao;

	public void setUserAgentDao(UserAgentDao userAgentDao) {
		this.userAgentDao = userAgentDao;
	}
	
	public List<UserAgent> getAll(){
		List list = (List)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_USER_AGENT_LIST);
		while (list == null) {
			list = userAgentDao.getAll();
			CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).put(Constants.CACHE_KEY_USER_AGENT_LIST, list);
		}
		return userAgentDao.getAll();
	}
	public List<UserAgent> getAllAvailable() {
		List<UserAgent> list = getAll();
		List<UserAgent> result = new ArrayList<UserAgent>();
		for(int i = 0; i < list.size(); i++){
			UserAgent ua = list.get(i);
			if(ua.getEnabled().compareTo(Short.valueOf((short)1)) == 0){
				result.add(ua);
			}
		}
		return result;
	}
	public void saveUserAgent(UserAgent ua) {
		userAgentDao.saveObject(ua);
	}

	public void deleteUserAgent(Long userAgentId) {
		userAgentDao.remove(UserAgent.class, userAgentId);
	}

}
